<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Evolution Strategy with Covariance Matrix Adaptation</title>
<link rel="stylesheet" href="../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Math Toolkit 4.2.1">
<link rel="up" href="../optimization.html" title="Chapter 11. Optimization">
<link rel="prev" href="random_search.html" title="Random Search">
<link rel="next" href="../poly.html" title="Chapter 12. Polynomials and Rational Functions">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="random_search.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../optimization.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../poly.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="math_toolkit.cma_es"></a><a class="link" href="cma_es.html" title="Evolution Strategy with Covariance Matrix Adaptation">Evolution Strategy with Covariance
    Matrix Adaptation</a>
</h2></div></div></div>
<h4>
<a name="math_toolkit.cma_es.h0"></a>
      <span class="phrase"><a name="math_toolkit.cma_es.synopsis"></a></span><a class="link" href="cma_es.html#math_toolkit.cma_es.synopsis">Synopsis</a>
    </h4>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">optimization</span><span class="special">/</span><span class="identifier">cma_es</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">math</span><span class="special">::</span><span class="identifier">optimization</span> <span class="special">{</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">ArgumentContainer</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">cma_es_parameters</span> <span class="special">{</span>
    <span class="keyword">using</span> <span class="identifier">Real</span> <span class="special">=</span> <span class="keyword">typename</span> <span class="identifier">ArgumentContainer</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">;</span>
    <span class="identifier">ArgumentContainer</span> <span class="identifier">lower_bounds</span><span class="special">;</span>
    <span class="identifier">ArgumentContainer</span> <span class="identifier">upper_bounds</span><span class="special">;</span>
    <span class="identifier">size_t</span> <span class="identifier">max_generations</span> <span class="special">=</span> <span class="number">1000</span><span class="special">;</span>
    <span class="identifier">ArgumentContainer</span> <span class="keyword">const</span> <span class="special">*</span> <span class="identifier">initial_guess</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">;</span>
    <span class="comment">// If zero, choose the default from the reference.</span>
    <span class="identifier">size_t</span> <span class="identifier">population_size</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    <span class="identifier">Real</span> <span class="identifier">learning_rate</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
<span class="special">};</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">ArgumentContainer</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Func</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">URBG</span><span class="special">&gt;</span>
<span class="identifier">ArgumentContainer</span> <span class="identifier">cma_es</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">Func</span> <span class="identifier">cost_function</span><span class="special">,</span>
                         <span class="identifier">cma_es_parameters</span><span class="special">&lt;</span><span class="identifier">ArgumentContainer</span><span class="special">&gt;</span> <span class="keyword">const</span> <span class="special">&amp;</span><span class="identifier">params</span><span class="special">,</span>
                         <span class="identifier">URBG</span> <span class="special">&amp;</span><span class="identifier">gen</span><span class="special">,</span>
                         <span class="identifier">std</span><span class="special">::</span><span class="identifier">invoke_result_t</span><span class="special">&lt;</span><span class="identifier">Func</span><span class="special">,</span> <span class="identifier">ArgumentContainer</span><span class="special">&gt;</span> <span class="identifier">value_to_reach</span>
                           <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">invoke_result_t</span><span class="special">&lt;</span><span class="identifier">Func</span><span class="special">,</span> <span class="identifier">ArgumentContainer</span><span class="special">&gt;&gt;::</span><span class="identifier">quiet_NaN</span><span class="special">(),</span>
                         <span class="identifier">std</span><span class="special">::</span><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="special">*</span><span class="identifier">cancellation</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">,</span>
                         <span class="identifier">std</span><span class="special">::</span><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">invoke_result_t</span><span class="special">&lt;</span><span class="identifier">Func</span><span class="special">,</span> <span class="identifier">ArgumentContainer</span><span class="special">&gt;&gt;</span> <span class="special">*</span><span class="identifier">current_minimum_cost</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">,</span>
                         <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">ArgumentContainer</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">invoke_result_t</span><span class="special">&lt;</span><span class="identifier">Func</span><span class="special">,</span> <span class="identifier">ArgumentContainer</span><span class="special">&gt;&gt;&gt;</span> <span class="special">*</span><span class="identifier">queries</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">);</span>

<span class="special">}</span> <span class="comment">// namespaces</span>
</pre>
<p>
      The <code class="computeroutput"><span class="identifier">cma_es</span></code> optimizer searches
      for a global minimum of a function. Our implementation attempts to follow "The
      CMA evolution strategy: A tutorial" exactly.
    </p>
<h4>
<a name="math_toolkit.cma_es.h1"></a>
      <span class="phrase"><a name="math_toolkit.cma_es.parameters"></a></span><a class="link" href="cma_es.html#math_toolkit.cma_es.parameters">Parameters</a>
    </h4>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          <code class="computeroutput"><span class="identifier">lower_bounds</span></code>: A container
          representing the lower bounds of the optimization space along each dimension.
          The <code class="computeroutput"><span class="special">.</span><span class="identifier">size</span><span class="special">()</span></code> of the bounds should return the dimension
          of the problem.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">upper_bounds</span></code>: A container
          representing the upper bounds of the optimization space along each dimension.
          It should have the same size of <code class="computeroutput"><span class="identifier">lower_bounds</span></code>,
          and each element should be &gt;= the corresponding element of <code class="computeroutput"><span class="identifier">lower_bounds</span></code>.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">initial_guess</span></code>: An optional
          guess for where we should start looking for solutions.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">max_generations</span></code>: The maximum
          number of generations before giving up.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">population_size</span></code>: The number
          of function calls per generation. Defaults to <code class="computeroutput"><span class="number">4</span>
          <span class="special">+</span> <span class="number">3l</span><span class="identifier">og</span><span class="special">(</span><span class="identifier">D</span><span class="special">)</span></code>, where <code class="computeroutput"><span class="identifier">D</span></code>
          is the dimension.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">learning_rate</span></code>: Unlikely
          to be necessary-refer to the reference for when this should be changed.
        </li>
</ul></div>
<h4>
<a name="math_toolkit.cma_es.h2"></a>
      <span class="phrase"><a name="math_toolkit.cma_es.the_function"></a></span><a class="link" href="cma_es.html#math_toolkit.cma_es.the_function">The
      function</a>
    </h4>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">ArgumentContainer</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Func</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">URBG</span><span class="special">&gt;</span>
<span class="identifier">ArgumentContainer</span> <span class="identifier">cma_es</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">Func</span> <span class="identifier">cost_function</span><span class="special">,</span>
                         <span class="identifier">cma_es_parameters</span><span class="special">&lt;</span><span class="identifier">ArgumentContainer</span><span class="special">&gt;</span> <span class="keyword">const</span> <span class="special">&amp;</span><span class="identifier">params</span><span class="special">,</span>
                         <span class="identifier">URBG</span> <span class="special">&amp;</span><span class="identifier">gen</span><span class="special">,</span>
                         <span class="identifier">std</span><span class="special">::</span><span class="identifier">invoke_result_t</span><span class="special">&lt;</span><span class="identifier">Func</span><span class="special">,</span> <span class="identifier">ArgumentContainer</span><span class="special">&gt;</span> <span class="identifier">value_to_reach</span>
                            <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">numeric_limits</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">invoke_result_t</span><span class="special">&lt;</span><span class="identifier">Func</span><span class="special">,</span> <span class="identifier">ArgumentContainer</span><span class="special">&gt;&gt;::</span><span class="identifier">quiet_NaN</span><span class="special">(),</span>
                         <span class="identifier">std</span><span class="special">::</span><span class="identifier">atomic</span><span class="special">&lt;</span><span class="keyword">bool</span><span class="special">&gt;</span> <span class="special">*</span><span class="identifier">cancellation</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">,</span>
                         <span class="identifier">std</span><span class="special">::</span><span class="identifier">atomic</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">invoke_result_t</span><span class="special">&lt;</span><span class="identifier">Func</span><span class="special">,</span> <span class="identifier">ArgumentContainer</span><span class="special">&gt;&gt;</span> <span class="special">*</span><span class="identifier">current_minimum_cost</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">,</span>
                         <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special">&lt;</span><span class="identifier">ArgumentContainer</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">invoke_result_t</span><span class="special">&lt;</span><span class="identifier">Func</span><span class="special">,</span> <span class="identifier">ArgumentContainer</span><span class="special">&gt;&gt;&gt;</span> <span class="special">*</span><span class="identifier">queries</span> <span class="special">=</span> <span class="keyword">nullptr</span><span class="special">)</span>
</pre>
<p>
      Parameters:
    </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
          <code class="computeroutput"><span class="identifier">cost_function</span></code>: The cost
          function to be minimized.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">params</span></code>: The parameters
          to the algorithm as described above.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">gen</span></code>: A uniform random bit
          generator, like <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">mt19937_64</span></code>.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">value_to_reach</span></code>: An optional
          value that, if reached, stops the optimization. This is the most robust
          way to terminate the calculation, but in most cases the optimal value of
          the cost function is unknown. If it is, use it! Physical considerations
          can often be used to find optimal values for cost functions.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">cancellation</span></code>: An optional
          atomic boolean to allow the user to stop the computation and gracefully
          return the best result found up to that point. N.B.: Cancellation is not
          immediate; the in-progress generation finishes.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">current_minimum_cost</span></code>: An
          optional atomic variable to store the current minimum cost during optimization.
          This allows developers to (e.g.) plot the progress of the minimization
          over time and in conjunction with the <code class="computeroutput"><span class="identifier">cancellation</span></code>
          argument allow halting the computation when the progress stagnates.
        </li>
<li class="listitem">
          <code class="computeroutput"><span class="identifier">queries</span></code>: An optional vector
          to store intermediate results during optimization. This is useful for debugging
          and perhaps volume rendering of the objective function after the calculation
          is complete.
        </li>
</ul></div>
<p>
      Returns:
    </p>
<p>
      The <code class="computeroutput"><span class="identifier">ArgumentContainer</span></code> corresponding
      to the minimum cost found by the optimization.
    </p>
<h5>
<a name="math_toolkit.cma_es.h3"></a>
      <span class="phrase"><a name="math_toolkit.cma_es.examples"></a></span><a class="link" href="cma_es.html#math_toolkit.cma_es.examples">Examples</a>
    </h5>
<p>
      An example exhibiting graceful cancellation and progress observability can
      be studied in <a href="../../../example/cma_es_example.cpp" target="_top">cma_es_example.cpp</a>.
    </p>
<h5>
<a name="math_toolkit.cma_es.h4"></a>
      <span class="phrase"><a name="math_toolkit.cma_es.references"></a></span><a class="link" href="cma_es.html#math_toolkit.cma_es.references">References</a>
    </h5>
<p>
      Hansen, N. (2016). The CMA evolution strategy: A tutorial. arXiv preprint arXiv:1604.00772.
    </p>
</div>
<div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="random_search.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../optimization.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../poly.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
